class CreateLeagueTables < ActiveRecord::Migration
  def self.up
    begin people_up() rescue puts "Failed to Create: " + $! end
    begin players_up() rescue puts "Failed to Create: " + $! end
    begin teams_up() rescue puts "Failed to Create: " + $! end
    begin leagues_up() rescue puts "Failed to Create: " + $! end
  end

  def self.down
    begin drop_table :teams  rescue puts "Failed to drop: " + $! end
    begin drop_table :leagues rescue puts "Failed to drop: " + $! end
    begin drop_table :people rescue puts "Failed to drop: " + $! end

    begin drop_table :players rescue puts "Failed to drop: " + $! end
    begin drop_table :links_players rescue puts "Failed to drop: " + $! end
    begin drop_table :images_players rescue puts "Failed to drop: " + $! end
    begin drop_table :players_tags rescue puts "Failed to drop: " + $! end
    begin drop_table :people_players rescue puts "Failed to drop: " + $! end
    begin drop_table :honors_players rescue puts "Failed to drop: " + $! end

    begin drop_table :images_teams  rescue puts "Failed to drop: " + $! end
    begin drop_table :players_teams  rescue puts "Failed to drop: " + $! end
    begin drop_table :links_teams  rescue puts "Failed to drop: " + $! end
    begin drop_table :tags_teams  rescue puts "Failed to drop: " + $! end
    begin drop_table :leagues_teams rescue puts "Failed to drop: " + $! end

    begin drop_table :leagues_locations rescue puts "Failed to drop: " + $! end
    begin drop_table :locations_teams rescue puts "Failed to drop: " + $! end
  end

  def self.people_up
    create_table :people do |t|
      t.string :name
      t.string :last
      t.string :first
    end
  end


  def self.players_up
    create_table :players do |t|
      t.text     :handle
      t.datetime :start_date
      t.boolean  :is_active
      t.timestamps
    end

    create_table :people_players, :id => false do |t|
      t.integer :player_id
      t.integer :person_id
    end

    create_table :links_players, :id => false do |t|
      t.integer :link_id
      t.integer :player_id
    end

    create_table :images_players, :id => false do |t|
      t.integer :image_id
      t.integer :player_id
    end

    create_table :players_tags, :id => false do |t|
      t.integer :tag_id
      t.integer :player_id
    end

    create_table :honors_players, :id => false do |t|
      t.integer :honors
      t.integer :players
    end

  end

  def self.teams_up
    create_table :teams do |t|
      t.string :name
      t.text   :description
      t.timestamps
    end

    create_table :players_teams, :id => false do |t|
      t.integer :player_id
      t.integer :team_id
    end

    create_table :tags_teams, :id => false do |t|
      t.integer :tag_id
      t.integer :team_id
    end

    create_table :links_teams, :id => false do |t|
      t.integer :link_id
      t.integer :team_id
    end

    create_table :images_teams, :id => false do |t|
      t.integer :image_id
      t.integer :team_id
    end

    create_table :locations_teams, :id => false do |t|
      t.integer :location_id
      t.integer :team_id
    end
  end


  def self.leagues_up
    create_table :leagues do |t|
      t.string :name
      t.text   :description
      t.text   :abbrev
      t.timestamps
    end

    create_table :leagues_teams, :id => false do |t|
      t.integer :league_id
      t.integer :team_id
    end

    create_table :leagues_locations, :id => false do |t|
      t.integer :league_id
      t.integer :location_id
    end
  end
end
